package com.idp.system.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.idp.common.constants.SystemParam;
import com.idp.common.error.SystemException;
import com.idp.common.utils.IOResUtil;
import com.idp.system.dto.SysFileDto;
import com.idp.system.service.CommonService;
import com.idp.system.service.SysFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.Calendar;

@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    private SystemParam systemParam;
    @Autowired
    private SysFileService sysFileService;

    private static Logger log = LoggerFactory.getLogger(CommonServiceImpl.class);

    @Override
    public void showFile(String id, HttpServletResponse response) {

        SysFileDto fileDto = sysFileService.getById(id);

        String filePath = systemParam.getFileRootDir() + fileDto.getFilePath();

        File file = new File(filePath);

        if(file.exists()) {
            String fileName = FileUtil.getName(file);
            try {
                IOResUtil.toResponse(new FileInputStream(file), fileName, response);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        else {

            throw new SystemException("文件不存在");
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String uploadFile(MultipartFile file, String backType) {

        String basePath = systemParam.getFileRootDir();

        Calendar c = Calendar.getInstance();

        StringBuilder filePath = new StringBuilder();
        filePath.append("/").append(c.get(Calendar.YEAR))
                .append("/").append(c.get(Calendar.MONTH)+1)
                .append("/").append(c.get(Calendar.DATE));

        String suffix = FileUtil.getSuffix(file.getOriginalFilename());

        filePath.append("/").append(IdUtil.simpleUUID()).append(".").append(suffix);

        File localFile = FileUtil.touch(basePath + filePath.toString());

        try {
            file.transferTo(localFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        SysFileDto fileDto = new SysFileDto();
        fileDto.setId(IdUtil.simpleUUID());
        fileDto.setFileName(file.getOriginalFilename());
        fileDto.setFileType(suffix.toLowerCase());
        fileDto.setFilePath(filePath.toString());
        sysFileService.add(fileDto);

        if("editor".equals(backType)) {

            String url = systemParam.getFileServerPath() + "/common/showFile?id=" + fileDto.getId();

            return url;
        }

        return fileDto.getId();
    }

}
